﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Web.Controls;
using BySoft.Server.Consts;
using BySoft.Server.Common;
using ZCZP.Web.Site;

namespace BySoft.Web.Site.OutSource
{
    public partial class OutBackMatList : AuthBasePage
    {
        private string m_strTbl = "BS_OutBackMat";
        private string m_strMainKey = "BillID";

        #region 变量定义
        int m_count = 0;
        int iPageIndex = 0;

        private DataTable DtOutBackMatListData
        {
            get
            {
                if (null == Session["OutBackMatListData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["OutBackMatListData"];
                }
            }
            set
            {
                Session["OutBackMatListData"] = value;
            }
        }

        private DataTable CreateDT()
        {
          
            return BsOutbackmat.CreateDT();
        }

        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        {
           
             string strFrom = CommonMethod.GetStartTime(cpFrom.Value);
            string strTo = CommonMethod.GetEndTime(cpTo.Value);
            DataTable dt = BsOutbackmat.SelectList(txtBillID.Text,txtWorkOrderNo.Text.Trim(),ddlSupplierID.SelectedValue, strFrom, strTo,  pageIndex, pcData.PageSize, out m_count);

            pcData.RecordCount = m_count;
            if (pageIndex > pcData.PageCount - 1)
            {
                pageIndex = pcData.PageCount - 1;
            }

            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
                //UIHelper.GroupRows1(grdResult, new int[] { 1, 2, 5, 6, 7, 8, 10, 11, 12, 13 });
                //UIHelper.GroupRows(grdResult, "lblEnrollApplyID", new int[] { 0 ,1, 2 });

                DisableCells(dt, new string[] { "ibtnChk", "ibtnDel", "ibtnEdi" });

               
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void DisableCells(DataTable dt, string[] cols)
        {


            for (int n = 0; n < dt.Rows.Count; n++)
            {
                DataRow dr = dt.Rows[n];
                if (CommonMethod.GetString(dr["CheckResult"]) != "")
                {
                    GridViewRow gvr = grdResult.Rows[n];
                    for (int i = 0; i < cols.Length; i++)
                    {
                        string col = cols[i];
                 
                        gvr.FindControl(col).Visible=false;
                        //gvr.Cells[i].Controls[0].Visible = false;

                    }
                }


            }
        }
       

        private void InitData()
        {
            BindData(0);
        }

        /// <summary>
        /// 绑定ddl
        /// </summary>
        private void InitDDL()
        {
            DataTable dt = MdSupplier.GetMdSupplierAll().Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.ddlSupplierID.DataSource = dt;
                this.ddlSupplierID.DataTextField = "SupplierName";
                this.ddlSupplierID.DataValueField = "SupplierID";
                this.ddlSupplierID.DataBind();
            }
            UIHelper.InsertFirstItem(ddlSupplierID);
        }
        #endregion

        #region 控件事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RemoveSession();
                InitDDL();
                
                InitData();
            }
        }

        private void RemoveSession()
        {
            List<string> lst = GetSession();
            lst.Add("OutBackMatIPageIndex");
            lst.Add("OutBackMatListData");
            lst.Add("IOutBackMatCount");

            ClearSession(lst);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
             BindData(e.PageIndex);
        }

        /// <summary>
        ///   行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            string[] strs = strCode.Split(',');
            //string strMention = "";
            switch (e.CommandName)
            {
                case "viewdata":
                    Response.Redirect(string.Format("OutBackMatView.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "edi":
                    if (BsOutbackmat.CheckUsed(strCode))
                    {
                        UIHelper.AlertMessage(this.Page, "已经生成后置单据,不能编辑");
                        return;
                    }
                    Response.Redirect(string.Format("OutBackMatEdit.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "del":
                    if (BsOutbackmat.CheckUsed(strCode))
                    {
                        UIHelper.AlertMessage(this.Page, "已经生成后置单据,不能删除");
                        return;
                    }
                    BsOutbackmat.Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;

                case "chk":
                   
                    UIHelper.RunScript(this, "OpenProcessShow('" + strCode + "')");
                    BindData(0);
                  
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion
            }
        }

        protected void btnSingerCheck_Click(object sender, EventArgs e)
        {
            BindData(0);
            UIHelper.AlertMessage(this, "操作成功！");
        }
        #endregion
    }
}

